<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Inc. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/documents
 */
class appextensionsController extends appRain_Base_Core
{
    public $name = 'appExtensions';

    /**
     * This function will reander the home page
     *
     * @return null
     */
    public function indexAction($cid = null)
    {
		$this->pageNameToMetaInfo('Extensions');

		$extcatlist = $this->CategorySet('extension-cat')->findAll('1 ORDER BY title ASC');
		$this->set('extcatlist',$extcatlist);

		if(isset($cid) && $cid != "")
		{
			$extlist = App::InformationSet('extension-post')
						  ->setPagination('Yes')
						  ->setLimit(20)
						  ->setNextCaption('Next >>')
						  ->setPrevCaption('<< Prev')
			              ->findAllByCategory($cid);

		}
		else
		{
			if(isset($this->get['filterby']))
			{
				$extlist = App::InformationSet('extension-post')
							  ->setPagination('Yes')
							  ->setLimit(20)
							  ->setHLink("?filterby=" . $this->get['filterby'])
							  ->setNextCaption('Next >>')
							  ->setPrevCaption('<< Prev')
							  ->findAll("1 ORDER BY id DESC",
									Array(
									   Array("title"=>"%{$this->get['filterby']}%","cnd"=>"LIKE")
								   )
								);

			}
			else
			{
				$extlist = App::InformationSet('extension-post')
						->setPagination('Yes')
						->setLimit(20)
						->setNextCaption('Next >>')
						->setPrevCaption('<< Prev')
						->findAll("1 ORDER BY id DESC");
			}
		}

		$this->set('extlist',$extlist);


        $this->set("section_title","Extensions");
        $this->set("selected","plugins");
    }

	public function downloadAction($action=null,$id=null)
	{
		$extension = App::InformationSet()->findById($id);
		if(empty($extension) || !file_exists($this->basedir("/uploads/filemanager/" . $extension['source-code'])))
		{
			App::Helper('Config')->transfer($this->baseurl('/extensions'),'Sorry! Server is busy, Please try after some time');
		}

		$this->InformationSet('extension-post')->setId($extension['id'])->setDownloaded(($extension['downloaded']+1))->Save();

		if($action!='man')	$this->redirect("/uploads/filemanager/{$extension['source-code']}");
		else $this->redirect("/uploads/filemanager/{$extension['manual']}");
		exit;
	}
}
